package com.chookss.home.audit.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.audit.clockIn.ClockInAskHolidayDetailActivity;
import com.chookss.home.audit.clockIn.ClockInReissueCardDetailActivity;
import com.chookss.home.audit.resign.EntryDetailActivity;
import com.chookss.home.audit.resign.ResignDetailActivity;
import com.chookss.home.audit.upgrade.UpgradeDetailActivity;
import com.chookss.home.entity.WorkNewEntity;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClockInAuditAdapter extends BaseQuickAdapter<WorkNewEntity, BaseViewHolderEx> {
    private Activity context;

    public ClockInAuditAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<WorkNewEntity> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, WorkNewEntity entity) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        TextView tvTime = helper.getView(R.id.tvTime);
        TextView tvStatus = helper.getView(R.id.tvStatus);
        ImageView ivType = helper.getView(R.id.ivType);
        TextView tvInit = helper.getView(R.id.tvInit);
        LinearLayout llType = helper.getView(R.id.llType);
        tvTitle.setText(entity.getProjectName());
        if (entity.isSelect()) {
            ivType.setBackgroundResource(R.drawable.icon_fx_select);
        } else {
            ivType.setBackgroundResource(R.drawable.shape_circle_b5b5b5_20);
        }
        //缺失数据，需要接口补充
        if ("4".equals(entity.getWorkFlowType())) {
            //请假
//                tvTime.setText(ClockInUtils.changeTime(entity.getParams().getStartTime()) + "-" + ClockInUtils.changeTime(entity.getParams().getEndTime()));
            tvTime.setText(entity.getParams().getStartTime() + " - " + entity.getParams().getEndTime());
            tvStatus.setText("请假时长：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getDays()) + "天");
            if (Utils.isNull(entity.getParams().getRemark())) {
                tvInit.setText("无");
            } else {
                tvInit.setText(entity.getParams().getRemark());
            }
        } else if ("5".equals(entity.getWorkFlowType())) {
            //外勤
            tvTime.setText(entity.getCreateTime());
            if (Utils.isNull(entity.getParams().getShangbanCd())) {
                tvStatus.setText("外勤");
                if (!Utils.isNull(entity.getParams().getShangbanWaiqinRemark())) {
                    tvInit.setText(entity.getParams().getShangbanWaiqinRemark());
                } else if (!Utils.isNull(entity.getParams().getXiabanWaiqinRemark())) {
                    tvInit.setText(entity.getParams().getXiabanWaiqinRemark());
                } else {
                    tvInit.setText("无");
                }
            } else {
                if ("1".equals(entity.getParams().getShangbanCd())) {
                    tvStatus.setText("上班外勤");
                    if (Utils.isNull(entity.getParams().getShangbanWaiqinRemark())) {
                        tvInit.setText("无");
                    } else {
                        tvInit.setText(entity.getParams().getShangbanWaiqinRemark());
                    }
                } else if ("0".equals(entity.getParams().getShangbanCd())) {
                    tvStatus.setText("下班外勤");
                    if (Utils.isNull(entity.getParams().getXiabanWaiqinRemark())) {
                        tvInit.setText("无");
                    } else {
                        tvInit.setText(entity.getParams().getXiabanWaiqinRemark());
                    }
                } else {
                    tvStatus.setText("外勤");
                    tvInit.setText("无");
                }
            }

        } else if ("14".equals(entity.getWorkFlowType())) {
            //离职
            tvTime.setText("申请时间："+entity.getCreateTime());
            tvStatus.setText("");
            if (Utils.isNull(entity.getParams().getRemark())) {
                tvInit.setText("无");
            } else {
                tvInit.setText(entity.getParams().getRemark());
            }
        } else if ("15".equals(entity.getWorkFlowType())) {
            //晋升
            tvTime.setText("申请时间："+entity.getCreateTime());
            tvStatus.setText("");
            if (Utils.isNull(entity.getParams().getRemark())) {
                tvInit.setText("无");
            } else {
                tvInit.setText(entity.getParams().getRemark());
            }
        } else if ("18".equals(entity.getWorkFlowType())) {
            //入职
            tvTime.setText("申请时间："+entity.getCreateTime());
            tvStatus.setText("");
            if (Utils.isNull(entity.getParams().getRemark())) {
                tvInit.setText("无");
            } else {
                tvInit.setText(entity.getParams().getRemark());
            }
        }  else {
            //补卡
            if (!Utils.isNull(entity.getParams().getStartTime())) {
                //说明补卡的是上午
                tvTime.setText(entity.getParams().getStartTime());
                if (Utils.isNull(entity.getParams().getShangbanBukaRemark())) {
                    tvInit.setText("无");
                } else {
                    tvInit.setText(entity.getParams().getShangbanBukaRemark());
                }
                tvStatus.setText("上班补卡");
            } else {
                //说明补卡的是下去\
                tvTime.setText(entity.getParams().getEndTime());
                if (Utils.isNull(entity.getParams().getXiabanBukaRemark())) {
                    tvInit.setText("无");
                } else {
                    tvInit.setText(entity.getParams().getXiabanBukaRemark());
                }
                tvStatus.setText("下班补卡");
            }
        }
        llType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, String> map = new HashMap<>();
                map.put("ClockInAudit_position", helper.getItemPosition() + "");
                EventBus.getDefault().post(new MyEvent(map));
            }
        });
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                if ("4".equals(entity.getWorkFlowType())) {
                    Intent intent = new Intent(context, ClockInAskHolidayDetailActivity.class);
                    intent.putExtra("data", entity);
                    context.startActivity(intent);
                }  else if ("14".equals(entity.getWorkFlowType())) {
                    Intent intent = new Intent(context, ResignDetailActivity.class);
                    intent.putExtra("data", entity);
                    context.startActivity(intent);
                } else if ("15".equals(entity.getWorkFlowType())) {
                    Intent intent = new Intent(context, UpgradeDetailActivity.class);
                    intent.putExtra("data", entity);
                    context.startActivity(intent);
                } else if ("18".equals(entity.getWorkFlowType())) {
                    Intent intent = new Intent(context, EntryDetailActivity.class);
                    intent.putExtra("data", entity);
                    context.startActivity(intent);
                }  else {
                    Intent intent = new Intent(context, ClockInReissueCardDetailActivity.class);
                    intent.putExtra("data", entity);
                    intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
                    context.startActivity(intent);
                }
            }
        });
    }
}
